package com.bstek.mes.emp;

import com.bstek.dorado.annotation.PropertyDef;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;

@Entity
@Table(name="test_employee")
public class Employee implements Serializable {
	
	private static final long serialVersionUID = 1L;
	@Id
	@Column(name = "ID", length = 32, nullable = false)
	@PropertyDef(label = "主键")
	private String id;
	
	@Column(name = "name", length = 60, nullable = false)
	@PropertyDef(label = "姓名")
	private String name;
	
	@Column(name = "age")
	@PropertyDef(label = "年龄")
	private int age;
	
	@Column(name = "sex")
	@PropertyDef(label = "性别")
	private int sex;
	
	@Column(name = "salary",precision = 20, scale = 2)
	@PropertyDef(label = "收入")
	private BigDecimal salary;
	
	@Column(name = "hobby", length = 100)
	@PropertyDef(label = "兴趣爱好")
	private String hobby;
	
	@Column(name = "remark", length = 200)
	@PropertyDef(label = "备注")
	private String remark;
	
	@Column(name = "create_time")
	@PropertyDef(label = "创建时间")
	private Date createTime;
	
	@Column(name = "create_user")
	@PropertyDef(label = "创建人")
	private String createUser;

	/**
	 * 该字段是为了解决前台javaScript Number类型超过了16位数字时，显示异常的问题
	 * 在此，前台使用字符串类型，后台java使用BigDecimal类型，oracle数据库使用Number类型
	 * BigDecimal salary：对应于后台，和持久化到数据库；
	 * String salaryShow：对应于前台展示；
	 */
	@Transient
	@PropertyDef(label = "收入")
	private String salaryShow;
	
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public int getSex() {
		return sex;
	}

	public void setSex(int sex) {
		this.sex = sex;
	}
	
	public BigDecimal getSalary() {
		return salary;
	}

	public void setSalary(BigDecimal salary) {
		//this.salary=salary;
	}
	/**
	 * 把后台的“收入”字段salary，转换成字符串，赋值给前台展示
	 * @author hans
	 */
	public String getSalaryShow() {
		NumberFormat nf=new DecimalFormat("###,###.00");//格式化，千分位
		return this.salary==null?"":nf.format(this.salary);
	}
	/**
	 * 把前台“收入”字段salaryShow的值转换成BigDecimal，赋值到后台并入库
	 * @author hans
	 * @return
	 */
	public void setSalaryShow(String salaryShow) {
		if(StringUtils.hasText(salaryShow)){
			this.salary = new BigDecimal(salaryShow).setScale(BigDecimal.ROUND_HALF_UP);
		}else{
			this.salary=null;
		}
		this.salaryShow = salaryShow;
	}

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public String getRemark() {
		return remark;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getCreateUser() {
		return createUser;
	}

	public void setCreateUser(String createUser) {
		this.createUser = createUser;
	}
	
	
	
}
